﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace ThanNhanTao.Server.BLL
{
    public class Dm_NgheNghiepBLL
    {
        bvdn_thannhantaoEntities entities;
        public Dm_NgheNghiepBLL()
        {
            entities = ThanNhanTaoGlobalServer.bvdn_thannhantaoEntities;
        }

        public List<Dm_NgheNghiep> RetrieveAll()
        {
            return entities.Dm_NgheNghiep.ToList();
        }

        public List<Dm_NgheNghiep> RetrieveAllKichHoat()
        {
            return entities.Dm_NgheNghiep.Where(u => u.KichHoat).ToList();
        }

        public void Save(string maNgheNghiep, string tenNgheNghiep, string ghiChu, bool kichHoat, bool themMoi)
        { 
            if(themMoi)
            {
                Dm_NgheNghiep dm = new Dm_NgheNghiep();
                dm.MaNgheNghiep = maNgheNghiep;
                dm.TenNgheNghiep = tenNgheNghiep;
                dm.GhiChu = ghiChu;
                dm.KichHoat = kichHoat;
                entities.AddToDm_NgheNghiep(dm);
                entities.SaveChanges();
            }
            else
            {
                Dm_NgheNghiep updm = entities.Dm_NgheNghiep.Where(u => u.MaNgheNghiep == maNgheNghiep).First();
                updm.TenNgheNghiep = tenNgheNghiep;
                updm.GhiChu = ghiChu;
                updm.KichHoat = kichHoat;
                entities.SaveChanges();
            }
        }

        public bool WasUsed(string maNgheNghiep)
        {
            return (entities.BenhNhans.Where(u => u.Dm_NgheNghiep.MaNgheNghiep == maNgheNghiep).FirstOrDefault() != null ? true : false);
        }

        public void Xoa(string maNgheNghiep)
        {
            Dm_NgheNghiep item = entities.Dm_NgheNghiep.First(u => u.MaNgheNghiep == maNgheNghiep);
            entities.DeleteObject(item);
            entities.SaveChanges();
        }

        public bool IsDuplicate(string maNgheNghiep)
        {
            return (entities.Dm_NgheNghiep.Where(u => u.MaNgheNghiep == maNgheNghiep).Count() > 0 ? true : false);
        }
    }
}
